package com.kaiwukj.android.ufamily.mvp.model;

import com.blankj.utilcode.util.GsonUtils;
import com.kaiwukj.android.mcas.integration.IRepositoryManager;
import com.kaiwukj.android.mcas.mvp.BaseModel;
import com.kaiwukj.android.ufamily.mvp.http.api.service.KeeperService;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.KeeperOrderResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.KeeperResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.keeper.KeeperContract;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;

public class KeeperModel extends BaseModel implements KeeperContract.Model {
    @Inject
    public KeeperModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public Observable<List<KeeperResult>> fetchCommunityKeeperList() {
        return mRepositoryManager.obtainRetrofitService(KeeperService.class)
                .fetchCommunityKeeperList()
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<List<KeeperResult>> fetchSysKeeperList() {
        return mRepositoryManager.obtainRetrofitService(KeeperService.class)
                .fetchSysKeeperResult()
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<Integer> bindKeeper(Map<String, Object> params) {
        return mRepositoryManager.obtainRetrofitService(KeeperService.class)
                .bindKeeper(getRequestBody(GsonUtils.toJson(params)))
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<KeeperResult> getMyKeeperInfo() {
        return mRepositoryManager.obtainRetrofitService(KeeperService.class)
                .getMyKeeper()
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<KeeperOrderResult>> fetchKeeperOrder(int page, int pageSize) {
        return mRepositoryManager.obtainRetrofitService(KeeperService.class)
                .fetchKeeperOrder(page, pageSize)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<Integer> addKeeperOrder(Integer keeperId, Integer keeperServiceId, String appointTime, String remark) {
        Map<String, Object> params = new HashMap<>();
        params.put("housekeeperAccountId", keeperId);
        params.put("housekeeperServiceId", keeperServiceId);
        //params.put("address", address);
        params.put("appointTime", appointTime);
        params.put("remarks", remark);
        return mRepositoryManager.obtainRetrofitService(KeeperService.class)
                .addKeeperOrder(getRequestBody(GsonUtils.toJson(params)))
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<Integer> addComplain(String content, Integer keeperId) {
        return mRepositoryManager.obtainRetrofitService(KeeperService.class)
                .addComplain(content, keeperId)
                .compose(RxUtils.handleCode());
    }
}
